package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1282.用户分组
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/8/12 9:34
 */
public class P1282GroupThePeopleGivenTheGroupSizeTheyBelongTo {
    public static void main(String[] args) {
        Solution solution = new P1282GroupThePeopleGivenTheGroupSizeTheyBelongTo().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<List<Integer>> groupThePeople(int[] groupSizes) {
//            //复制数组
//            int[] copyOf = Arrays.copyOf(groupSizes, groupSizes.length);
//            //数组排序
//            Arrays.sort(groupSizes);
            Map<Integer, List<Integer>> map = new HashMap<>();
            List<List<Integer>> ans = new ArrayList<>();

            for (int i = 0; i < groupSizes.length; i++) {
                List<Integer> integersList = map.getOrDefault(groupSizes[i], new ArrayList<>());
                if (integersList.size() == groupSizes[i]) {
                    //放入结果集
                    ans.add(integersList);
                    //清空数据
                    integersList = new ArrayList<>();
                }
                integersList.add(i);
                map.put(groupSizes[i], integersList);
            }

            //剩余的放入集合
            for (List<Integer> value : map.values()) {
                if (!value.isEmpty()) {
                    ans.add(value);
                }
            }
            return ans;
        }
    }
}
